<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('项目名称')" prop="dealName">
            <!-- <el-input v-model="form.dealName" :placeholder="$t('项目名称')" show-word-limit maxlength="250" /> -->
            <template>
              <!-- <el-link class="anywhere" type="primary" :underline="false" @click="editForm">
                {{ form.dealName }}
              </el-link> -->
              <router-link target="_blank" :to="{path:'/deal/home/dealBaseHome',query:{id:form.dealId,companyId:form.companyId}}" class="link_a">{{ form.dealName }} </router-link>
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('项目负责人')" prop="dealPerson">
            <!-- <el-input v-model="form.dealPerson" :placeholder="$t('项目负责人')" readonly /> -->
            <el-input v-model="personName" :placeholder="$t('项目负责人')" readonly />
          <!-- <UserSelect
              v-model="form.dealPerson"
              :placeholder="$t('用户')"
            /> -->
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('法务经理')" prop="lawManager">
            <!-- <el-input v-model="form.lawManager" :placeholder="$t('法务经理')" show-word-limit maxlength="50" /> -->
            <UserSelect v-model="form.lawManager" :placeholder="$t('用户')" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('申请内容')" prop="content">
            <el-input v-model="form.content" type="textarea" :placeholder="$t('申请内容')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <FileTable :list.sync="form.fileList" data-type="deal:nda" :btn-show="!otherBtnHide" />
        </el-col>
      </el-row>
    </el-form>
    <!-- <div slot="footer" class="dialog-footer" style="text-align: right;">
      <save-btn has-permi="system:nda" />
    </div> -->
    <!-- 添加或修改项目基本信息对话框 -->
    <el-dialog :title="$t('项目基本信息')" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealBaseForm ref="dealBaseForm" />
    </el-dialog>
  </div>
</template>

<script>
import {
  saveDealNda,
  getDealNda
} from '@/api/deal/nda/dealNda'
// import {
//   getByWfNodeId
// } from '@/api/deal/form/dealWfGeneral'
import {
  getDealBase
} from '@/api/base/deal/dealBase'

import dealBaseForm from '@/views/deal/base/dealBaseForm.vue'
export default {
  components: {

    dealBaseForm
  },
  data() {
    return {
      otherBtnHide: false,
      // 表单参数
      form: {
        fileList: []
      },
      personName: '',
      // 遮罩层
      loading: false,
      dealId: this.$route.query.id,
      // 表单校验
      rules: {
        lawManager: [{
          required: true,
          message: '必填信息',
          trigger: 'blur'
        }]
      },
      formOpen: false
    }
  },
  created() {},
  mounted() {
    this.$bus.$emit('async_done')
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getDealNda(data.id).then(response => {
          this.form = response.data
          getDealBase(this.dealId).then(res => {
            this.personName = res.data.personChargeName
            this.form.dealPerson = res.data.personCharge
          })
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      } else {
         this.reset()
          getDealBase(this.dealId).then(res => {
            this.personName = res.data.personChargeName
            this.form.dealPerson = res.data.personCharge
            this.form.dealName = res.data.dealName
          })
          this.loading = false
        // this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dealName: undefined,
        dealPerson: undefined,
        lawManager: undefined,
        content: undefined,
        createBy: undefined,
        updateBy: undefined,
        createTime: undefined,
        updateTime: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm(type) {
      console.log('type', type)
      console.log('this.form', this.form)
      return new Promise((resolve, reject) => {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          this.form.dealId = this.dealId
          saveDealNda(this.form).then(response => {
            if (response.code === 200) {
              if (type === 'process') {
                // 启动流程
                this.startProcess(response.data.id)
                // 点击的仅保存
              } else {
                this.msgSuccess(this.$t('保存成功'))
                // 关闭form页面
                this.closeForm()
              }
              resolve(response.data.id)
            } else {
              this.msgError(response.msg)
              reject()
            }
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
        })
      })
    },
    editForm() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.dealBaseForm.openLocation = 'bfHome'
        this.$refs.dealBaseForm.init(this.$route.query)
      })
    },
     // 启动流程
     startProcess: function(dataId) {
      console.log('dataId', dataId)
      this.loading = false
      // 启动流程时候传入的参数
      const processData = {}
      // 待办标题
      processData.title = '保密协议'
      // 业务表名
      processData.dataTable = 'deal_nda'
      // 业务ID
      processData.dataId = dataId
      // 业务表单地址(待办页面需要打开的页面)
      processData.formPath = '@/views/deal/nda/dealNdaForm.vue'
      // 菜单标识(根据菜单标识判断的走哪个流程)
      processData.menuKey = 'deal_nda'
      this.startProcessByMenu(processData, () => {
        this.form.actStatus = 'D'
        this.msgSuccess(this.$t('数据保存成功,流程启动成功'))
        this.closeForm()
      })
    }
  }
}
</script>
